package server;

/* ActionsServer
 * ---------------------------------------------------
 * Written by
 * Rebecca Van Bussel 6271316
 * October 24, 2013
 */

import java.io.FileNotFoundException;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JOptionPane;

import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import org.omg.PortableServer.POAPackage.ObjectNotActive;
import org.omg.PortableServer.POAPackage.ServantAlreadyActive;
import org.omg.PortableServer.POAPackage.WrongPolicy;

import centralRepository.ServerObject;
import centralRepository.CentralRepository;
import centralRepository.ServerObjectImpl;

public class Driver {

	//private static String[] serverNames={"SPVM", "SPL", "SPB"};
	private static int numberOfRunningServers = 3;

	private static int[][] portNums={{2020, 2021, 2022},
		{2023, 2024, 2025},
		{2026, 2027, 2028},
		{2029, 2030, 2031}};
	private static int[][] edPortNums={{2032, 2033, 2034},
		{2035, 2036, 2037},
		{2038, 2039, 2040},
		{2041, 2042, 2043}};
	private static int[][] lePortNums={{2044, 2045, 2046},
		{2047, 2048, 2049},
		{2050, 2051, 2052},
		{2053, 2054, 2055}};

	//private static int id=10000;
	//Make sure naming service is up and running first
	//COMMAND PROMPT: start orbd -ORBInitialPort 1050
	public static void main(String[] args) {
		CentralRepository.startClientServers();

		int currentInstance;
		try{
			int serverIndex = Integer.parseInt(JOptionPane.showInputDialog("Enter the server number"))-1;
			String serverName = JOptionPane.showInputDialog("Enter the server name (SPVM, SPB, SPL)");
			if(serverName.equals("SPVM")) {
				currentInstance = 0;
			} else if(serverName.equals("SPB")) {
				currentInstance = 1;
			} else {
				currentInstance = 2;
			}
			System.out.println(currentInstance);
			
			ServerObjectImpl.Status status = ServerObjectImpl.Status.RUNNING;


			if (serverIndex >= numberOfRunningServers) //waiting processes
			{
				status = ServerObjectImpl.Status.WAITING;
				new SuperServer(portNums[serverIndex][currentInstance], edPortNums[serverIndex][currentInstance], lePortNums[serverIndex][currentInstance], serverName, false, status, serverName+portNums[serverIndex][currentInstance], portNums[serverIndex][currentInstance]);
			}
			else {
				new SuperServer(portNums[serverIndex][currentInstance], edPortNums[serverIndex][currentInstance], lePortNums[serverIndex][currentInstance], serverName, false, status, serverName+portNums[serverIndex][currentInstance], portNums[serverIndex][currentInstance]);
			}

		} catch (Exception e) {
			System.err.println("ERROR: " + e);
			e.printStackTrace(System.out);
		}
		Timer timer = new Timer();

		//SET A TIMER TO RUN THE ALGORITHM EVERY 1 min
		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				CentralRepository.snapshot();
				
			}}, 1000*30);
	}

}
